package de.tsystems.jsfdict.beans;

import de.tsystems.jsfdict.api.TranslationService;
import de.tsystems.jsfdict.model.Translation;
import de.tsystems.jsfdict.model.Word;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.Serializable;
import java.util.List;

@Named("translationBean")
@Scope("session")
//@URLMapping(id = "translations", pattern = "/translations/#{translationBean.currentWord}/", viewId = "/faces/translations.xhtml")
@Controller
public class TranslationBean implements Serializable {

    @Inject
    @Named("translationService")
    private TranslationService translationService;
    private Word currentWord;
    private Word right;
    private Translation newTranslation = new Translation();


    public void setTranslationService(TranslationService translationService) {
        this.translationService = translationService;
    }

    public Word getCurrentWord() {
        return currentWord;
    }

    public void setCurrentWord(Word currentWord) {
        this.currentWord = currentWord;
    }

    public void setRight(Word right) {
        this.right = right;
    }

    public Word getRight() {
        return right;
    }

    public void setNewTranslation(Translation newTranslation) {
        this.newTranslation = newTranslation;
    }

    public Translation getNewTranslation() {
        return newTranslation;
    }

    public void createNew() {
        FacesContext context = FacesContext.getCurrentInstance();
        newTranslation.setLeft(currentWord);
        if (getTranslation(newTranslation.getRight()) != null) {
            FacesMessage msg =
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, "Duplicated", "This translation has already been added");
            context.addMessage(null, msg);
        } else {
            translationService.save(newTranslation);
            context.addMessage(
                null,
                new FacesMessage(
                    FacesMessage.SEVERITY_INFO,
                    "Successful",
                    String.format("Translation had been added successfully")
                )
            );

            newTranslation = new Translation();          //reset form
        }
    }

    private Translation getTranslation(Word word) {
        for (Translation translation : getTranslations()) {
            if (translation.getLeft().equals(currentWord) && translation.getRight().equals(word)) {
                return translation;
            }
            if (translation.getLeft().equals(word) && translation.getRight().equals(currentWord)) {
                return translation;
            }
        }
        return null;
    }

    public void removeTranslation(Word word) {
        Translation translation = getTranslation(word);
        if (translation != null) {
            translationService.remove(translation);
        }
    }

    public List<Word> getTranslWords() {
        return translationService.getTranslationWords(currentWord);
    }

    public List<Translation> getTranslations() {
        return translationService.listAll();
    }

}
